package com.project.aaos.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.project.aaos.pojo.ApproFileInfo;
import com.project.aaos.pojo.Student;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;

public class AAOSUtil {

    public static String nowString(){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyymmdd");
        String date = simpleDateFormat.format(System.currentTimeMillis());
        return date;
    }

    public static JSONObject httpCall(String url,Object param){

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();


        StringEntity entity = null;
        try {
            entity = new StringEntity(JSON.toJSONString(param),"utf8");
        } catch (Exception e) {
            e.printStackTrace();
        }

        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 从响应模型中获取响应实体
        HttpEntity responseEntity = response.getEntity();
        String result = null;
        if (responseEntity != null) {
            try {
                result = EntityUtils.toString(responseEntity);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            // 释放资源
            if (httpClient != null) {
                httpClient.close();
            }
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return JSON.parseObject(result);
    }
}
